package com.healthcloud.controller;

import com.healthcloud.entity.Patient;
import com.healthcloud.entity.SysUserEntity;
import com.healthcloud.entity.patient.PatientDto;
import com.healthcloud.exception.BusinessException;
import com.healthcloud.service.PatientService;
import com.healthcloud.service.SysUserService;
import com.healthcloud.utils.ApiRes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.Objects;

/**
 * @author wangdi
 * @since 2024/9/9
 */
@ApiIgnore
@Api(value = "示例说明", description = "个人档安接口")
@RestController
@RequestMapping(value = "/patient")
public class PatientController {
    @Autowired
    private PatientService patientService;

    @Autowired
    private SysUserService userService;

    /**
     * <p>添加病人</p>
     * @param patientDto
     * @return
     */
    @ApiOperation(value = "添加病人")
    @PostMapping(value = "/save")
    public ApiRes save(@RequestBody PatientDto patientDto) {
        //验证手机号,身份证合法性
/*        if (!MobileUtil.isPhone(patientDto.getMobile())){
            throw new BusinessException("请输入正确的手机号");
        }
        if (!IdCardUtil.IDCardValidate(patientDto.getIdCard())){
            throw new BusinessException("请输入正确的身份证号");
        }*/
        //验证码验证
//        mobileCodeService.validate(patientDto.getMobile(), patientDto.getCode());
        //判断病人是否存在,不存在则新增.
        Patient patient = patientService.findByIdCard(patientDto.getIdCard());
        if (patient==null){
            patient = new Patient();
            BeanUtils.copyProperties(patientDto, patient);
            patient.setCreatedTime(new Date());
            patient.setUpdatedTime(new Date());
            patient.setPhone(patientDto.getMobile());
            //积分平台为4
            patient.setSourceType(4);
            patient.setType(4);
            patientService.saveOrUpdate(patient);
        }
        //判断病人是否有用户,如果有判断是否相同,相同则登录,没有则进入下一步,不同则抛出异常
        if (patient.getUserId()!=null){
            SysUserEntity sysUser = userService.queryObjectByid(patient.getUserId().longValue());
            if (Objects.equals(sysUser.getUsername(), patientDto.getMobile())){
                return ApiRes.SUCCESS;
            }
            throw new BusinessException("用户手机号与身份证不符");
        }
        SysUserEntity sysUser = userService.queryByUserName(patientDto.getMobile());
        if (sysUser!=null){
            Patient oPatient = patientService.getById(sysUser.getId());
            if (oPatient==null){
                patient.setUserId(sysUser.getId().intValue());
                patientService.saveOrUpdate(patient);
                return ApiRes.SUCCESS;
            }
            throw new BusinessException("用户已存在或用户手机号与身份证不符");
        }
        //则新增并绑定关系
        sysUser = new SysUserEntity();
        sysUser.setUsername(patientDto.getMobile());
        sysUser.setMobile(patientDto.getMobile());
        sysUser.setRealName(patientDto.getRealname());
        userService.save(sysUser);
        patient.setUserId(sysUser.getId().intValue());
        patientService.saveOrUpdate(patient);
        return ApiRes.SUCCESS;
    }
}
